<?php
namespace Message;
use Swiftx\DataBase\Connect;

/**
 * ---------------------------------------------------------------------------------------------------------------
 * 中国移动通信SMS短信发送接口
 * ---------------------------------------------------------------------------------------------------------------
 * @author		胡永强  <odaytudio@gmailcom>
 * @since		2015-02-25
 * @copyright	Copyright (c) 2014-2015 Swiftx Inc.
 * ---------------------------------------------------------------------------------------------------------------
 */
class Sms{

    // 短信内容
    private $_content = null;
    // 目标号码
    private $_number = null;
    // 连接句柄
    private $_driver = null;
    // 接口编码
    private $_code = null;

    // 发送短信
    public function __construct($address, $code, $username, $password){
        $database = Connect::PdoMysql($address, 'mas', $username, $password);
        $database->Execute('SET NAMES latin1');
        $this->_driver = $database;
        $this->_code = $code;
    }

    /**
     * 设置短信内容
     * @param $value 短信内容
     * @param string $code 内容原编码，默认utf-8
     */
    public function Content($value, $code='utf-8'){
        $this->_content = iconv($code, 'gb2312', $value);
    }

    /**
     * 设置发送号码
     */
    public function Number(){
        if(func_num_args()==1) {
            $this->_number = func_get_arg(0);
        } else {
            $this->_number = func_get_args();
        }
    }

    /**
     * 发送短信
     */
    public function Send(){
        $this->_driver->Table('api_mt_'.$this->_code)->insert(array(
            'MOBILES' => is_array($this->_number)?implode(',',$this->_number):$this->_number,
            'CONTENT' => $this->_content,
        ));
    }

}
